<template>
	<view>
		<!-- 	<view class="bindtext">
			<text class="bindtext1">因授权接口变更，只能通过此方法绑定昵称和头像</text>
		</view> -->
		<form @submit="setUserinfo">
			<view style="margin: 50rpx 30rpx;">
				<button class="avatar-wrapper" @click="getUserInfo">
					<image class="avatar" :src="avatarUrl||'/static/user.png'"></image>
				</button>
			</view>

			<view style="margin: 50rpx 50rpx;">
				<input class="uni-input" name="nickname" focus placeholder="请填写用户昵称" type="nickname"
					v-model="nickname"></input>
			</view>

			<view style="width: 350rpx;margin-left: 200rpx;">
				<button type="primary" form-type="submit">修改用户信息</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: {},
				notice: "111111111",
				nickname: "",
				avatarUrl: "",
			}
		},
		async onShow() {
			let userinfo = vk.vuex.get('$user.userInfo')
			if (userinfo.nickname) {
				this.nickname = userinfo.nickname;
				this.avatarUrl = userinfo.avatar;
			}
		},
		methods: {
			async getUserInfo() {
				var that = this;
				tt.getUserProfile({
					success(res) {
						console.log('成功', res);
						uni.vk.userCenter.updateUser({
							data: {
								nickname: res.userInfo.nickName,
								avatar: res.userInfo.avatarUrl
							},
							success: (data) => {
								that.nickname = data.userInfo.nickname;
								this.avatarUrl = data.userInfo.avatar
								console.log("更新头像昵称成功", data.userInfo);
							}
						});
					},
					fail(res) {
						vk.toast('用户信息获取失败', 'none');
					},
				});
			},

			setUserinfo(e) {
				console.log("nickname=", e.detail.value.nickname);
				if (e.detail.value.nickname == '') {
					vk.toast('请填写用户名');
				} else {
					vk.userCenter.updateUser({
						data: {
							nickname: e.detail.value.nickname,
							avatar: this.avatarUrl
						},
						success: (data) => {
							// 成功后的逻辑
							vk.toast('修改成功');
							setTimeout(() => {
								uni.switchTab({
									url: "/pages/Tabbar/my/my"
								})
							}, 1000)
						}
					});
				}
			}
		}
	}
</script>

<style>
	.bindtext {
		background-color: #808080;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx 20rpx;
	}

	.bindtext1 {
		margin-left: 10rpx;
		color: #fff;
		font-size: 26rpx;
	}

	.avatar-wrapper {
		width: 50%;
		background-color: #e6e6e6;
	}

	.avatar {
		border: 0;
		margin-top: 20rpx;
		border-radius: 50%;
		width: 220rpx;
		height: 220rpx;
	}

	.uni-input {
		align-self: center;
		padding-left: 30rpx;
		height: 100rpx;
		border-radius: 5%;
		background-color: #e6e6e6;
	}
</style>